package com.xpec.c4.db.datatransfer;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpec.c4.db.datatransfer.logic.BasicTransferLogic;
import com.xpec.c4.db.datatransfer.logic.RemoveGoodsLogic;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.game.system.InitData;

public class DataTransferHandler {

	private static final Log log = LogFactory.getLog(DataTransferHandler.class);
	private static DataTransferHandler instance = new DataTransferHandler();

	private static HashMap<String , Class<? extends BasicTransferLogic>> registerMap = new HashMap<String , Class<? extends BasicTransferLogic>>(); 
	static {
		// 类加载时就进行初始化
//		setLogic(FindPetCP.class);
//		setLogic(LogUnrecvDefenceRecordLogic.class);
		setLogic(RemoveGoodsLogic.class);
//		setLogic(PlayerSpellLevelRefresh.class);
//		setLogic(MountSkillExpLevelChange.class);
//		setLogic(TalentResetLogic.class);
//		setLogic(QuestClearLogic.class);
//		setLogic(AccountPlayerSerialNumLogic.class);
//		setLogic(FirstTopupQuestLogic.class);
	}
	public static DataTransferHandler getInstance() {
		return instance;
	}
	
	public void init(){
		initHibernate();
		initBytes();
	}
	private void initHibernate(){
		//DB
		HibernateManager.getSession();
	
	}
	public void done(){
		HibernateManager.closeSession();
	}
	private void initBytes(){
		InitData.initAll();
	}
	public void procLogic(String[] args){
		if(args == null || args.length < 1){
			log.info("param error");
			return ;
		}
		String commandName = args[0];
		
		if(commandName == null || !registerMap.containsKey(commandName)){
			log.info("recv non register command:" + commandName);
			return;
		}
		try {
			BasicTransferLogic c = (BasicTransferLogic) registerMap.get(commandName).newInstance();
			if(c != null)
				c.executeLogic(args);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	private static void setLogic(Class<? extends BasicTransferLogic> logic) {
		BasicTransferLogic c;
		try {
			c = logic.newInstance();
			registerMap.put(c.getName(), logic);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
}
